<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>登入</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/style/admin.css" media="all">
  <link rel="stylesheet" href="/style/login.css" media="all">
</head>
<body>

<div class="layadmin-user-login layadmin-user-display-show" style="display: none;">
  <div class="layadmin-user-login-main">
    <div class="layadmin-user-login-box layadmin-user-login-header">
      <h2>登录</h2>
      <p>在线订餐与配送服务</p>
    </div>
    <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="username"></label>
        <input type="text" id="username" name="username" value="admin" lay-verify="required"
               placeholder="用户名" class="layui-input">
      </div>
      <div class="layui-form-item">
        <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="password"></label>
        <input type="password" id="password" name="password" value="admin" lay-verify="required|pass"
               placeholder="密码" class="layui-input">
      </div>
      <div class="layui-form-item">
        <div class="layui-row">
          <div class="layui-col-xs7">
            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="code"></label>
            <input type="text" id="code" name="code" placeholder="图形验证码"
                   class="layui-input">
          </div>
          <div class="layui-col-xs5">
            <div style="margin-left: 10px;">
              <img src="/getAuthCode" class="layadmin-user-login-codeimg">
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登 入</button>
      </div>
    </div>
  </div>
  
  <div class="layui-trans layadmin-user-login-footer">
    <p>© 2021 <a href="" target="_blank">zyt.com</a></p>
  </div>
</div>
<script src="/layui/layui.js"></script>
<script src="/jquery/jquery-3.5.1.min.js"></script>
<script>
  layui.use(['layer', 'form'], function () {
    let layer = layui.layer;
    const form = layui.form;
    form.verify({
      pass: [
        /^[\S]{5,12}$/
        , '密码必须5到12位，且不能出现空格'
      ]
    });
    
    // 重载
    form.on("submit(login)", (data) => {
      let code = $("#code").val();
      let username = $("#username").val();
      let password = $("#password").val();
      let loginData = "username=" + username + "&password=" + password + "&code=" + code;
      $.ajax({
        type: "post",
        data: loginData,
        url: "/api/login",
        dataType: "json",
        success: function (data) {
          if (data.code === 200) {
            layer.msg("登录成功！即将跳转主页！");
            setTimeout(function (){
              window.location = "/home";
            },1500)
          }
          if (data.code == 100) {
            layer.msg(data.message)
          }
        },
        error: function () {
          layer.msg("服务器未响应")
        }
      })
      return false;
    })
  });

</script>
</body>
</html>